package light.mvc.controller.hrs.front;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import light.mvc.controller.base.BaseController;
import light.mvc.framework.constant.GlobalConstant;
import light.mvc.pageModel.base.Json;
import light.mvc.pageModel.base.SessionInfo;
import light.mvc.pageModel.hrs.HRSMember;
import light.mvc.service.hrs.HRSMemberServiceI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/front/member")
public class HRSMemberFrontController extends BaseController{

	@Autowired 
	private HRSMemberServiceI memberService;
	
	@RequestMapping("/editUserPwd")
	@ResponseBody
	public Json editUserPwd(HttpServletRequest request,String oldPwd, String pwd) {
		SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_INFO_FRONT);
		Json j = new Json();
		try {
			memberService.editUserPwd(sessionInfo, oldPwd, pwd);
			j.setSuccess(true);
			j.setMsg("密码修改成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/register")
	@ResponseBody
	public Json register(HRSMember member) {
		Json j = new Json();
		HRSMember u = memberService.getByLoginName(member);
		if(u!=null){
			j.setMsg("用户名已存在!");
		}else{
			try {
				member.setStatus(0);
				memberService.add(member);
				j.setSuccess(true);
				j.setMsg("提交注册成功，请等待审核！");
			} catch (Exception e) {
				j.setMsg(e.getMessage());
			}
			
		}
		return j;
	}
	
	@ResponseBody
	@RequestMapping("/login")
	public Json login(HRSMember member, HttpSession session) {
		Json j = new Json();
		HRSMember sysuser = memberService.login(member);
		if (sysuser != null) {
			j.setSuccess(true);
			j.setMsg("登陆成功！");

			SessionInfo sessionInfo = new SessionInfo();
			sessionInfo.setId(sysuser.getId());
			sessionInfo.setLoginname(sysuser.getLoginName());
			sessionInfo.setName(sysuser.getRealName());
			sessionInfo.setRoleType(GlobalConstant.MEMBER);
			session.setAttribute(GlobalConstant.SESSION_INFO_FRONT, sessionInfo);
			sessionInfo.setSessionId(session.getId());
			j.setObj(sessionInfo);
		} else {
			j.setMsg("用户名或密码错误！");
		}
		return j;
	}

}